
package org.sopt.dev.ImgKnight.frontier.Executor;

import java.util.Map;

import org.jboss.netty.channel.Channel;

import org.sopt.dev.ImgKnight.frontier.Controller.AgentController;

public class UrlPicker implements Runnable {
	
	private AgentController agentController = null;
	
	private int delayMilliSec = -1;
	
	public UrlPicker(AgentController agentController, int delayMilliSec) {
		this.agentController = agentController;
		this.delayMilliSec = delayMilliSec;
	}

	@Override
	@SuppressWarnings("unchecked")
	public void run() {
		while( !Thread.currentThread().isInterrupted() ) {
			try {
				Channel[] agentReadyChannelSet = agentController.getCurrentChannel();
				if( agentReadyChannelSet != null && agentReadyChannelSet.length > 0 ) {
					for(Channel c : agentReadyChannelSet) {
						Map urlInfo = agentController.getSiteMapHashTable().take();
						if( urlInfo != null )
							c.write(urlInfo);
					}
				}
				else {
					Thread.yield();
				}
				Thread.sleep(delayMilliSec);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
				return;
			}
		}
	}
	
}
